﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Text;
using MM.LibraryAPI.SZF;
using MM.Common;

namespace WebAPI.SZF
{
    /// <summary>
    /// Summary description for NotifyReceiver
    /// </summary>
    public class NotifyReceiver : IHttpHandler
    {
        String privateKey = MM.LibraryAPI.SZF.Config.privateKey;
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                String version = context.Request.Params.Get("version"); //版本号
                String merId = context.Request.Params.Get("merId"); //商户ID
                String payMoney = context.Request.Params.Get("payMoney"); //支付金额
                String orderId = context.Request.Params.Get("orderId"); //订单号
                String payResult = context.Request.Params.Get("payResult"); //支付结果
                String privateField = context.Request.Params.Get("privateField");
                String payDetails = context.Request.Params.Get("payDetails");
                String md5String = context.Request.Params.Get("md5String"); //MD5校验串
                String signString = context.Request.Params.Get("signString"); //神州付支付系统对md5加密后的32位字符串(md5String)进行签名。

                /*
                 * MD5校验
                 * md5String =md5(version+merId+payMoney+orderId+payResult+privateField+payDetails+privateKey)
                 */
                String combineString = version + merId + payMoney
                    + orderId + payResult + privateField + payDetails + privateKey;
                String md5 = MM.Common.DESEncrypt.MD5Encoding(combineString);

                if (md5.Equals(md5String))
                {
                    if (!string.IsNullOrEmpty(orderId) && "1".Equals(payResult.Trim()))
                    {
                        MM.LibraryAPI.Common.SetOrderStatus(orderId);
                    }
                    context.Response.Write(orderId);
                }
                else
                {
                    context.Response.Write("验证失败");
                    return;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("神州付返回信息异常WebAPI.SZF：", ex);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public bool veriSig(String data, String sign)
        {

            X509Store store = new X509Store(StoreName.Root);
            store.Open(OpenFlags.ReadWrite);
            try
            {
                //for .Net Framework2.0(VS .NET 2005)
                X509Certificate2 certificate = new X509Certificate2("c:\\shenzhoufuPay.cer");
                /*
                //for .Net Framework2.0(VS .NET 2005)
                X509Certificate certificate = X509Certificate.CreateFromCertFile("c:\\shenzhoufuPay.cer");	
                */
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

                rsa.FromXmlString(certificate.PublicKey.Key.ToXmlString(false));
                if (rsa.VerifyData(Encoding.UTF8.GetBytes(data), "MD5", Convert.FromBase64String(sign)))
                {
                    Console.Write("签名认证通过！");
                    return true;
                }
                else
                {
                    Console.Write("签名认证失败！");
                    return false;
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                return false;
            }
        }
    }
}